//
//  YWnsertPhotoView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/13.
//

#import "YWnsertPhotoView.h"

@interface YWnsertPhotoView ()<UIGestureRecognizerDelegate>

//原始view
@property(nonatomic,strong)UIImageView *bgImageView;

/** 要绘制的图片 */
@property (nonatomic, strong) UIImage * ogrinalImage;


@end

@implementation YWnsertPhotoView

-(UIImageView *)bgImageView{
    if(_bgImageView == nil){
        UIImageView *bgImageView = [[UIImageView alloc] init];
        _bgImageView = bgImageView;
    }
    return _bgImageView;
}

//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    [self addSubview:self.bgImageView];
    self.clipsToBounds = YES;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}



//设置初始图片
- (void)setFirstImage:(UIImage *)image{
    
    _ogrinalImage = image;
    
    self.bgImageView.image = image;
    
}
//设置表情图片
-(void)setPhotoImage:(UIImage *)image{
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.frame = self.bounds;
    imageV.userInteractionEnabled = YES;
    [self addSubview:imageV];
    imageV.image = image;
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    
    //添加手势
    // pan
    // 拖拽手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
                                   initWithTarget:self action:@selector(pan:)];
    
    [imageV addGestureRecognizer:pan];
    
    // pinch
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    
    pinch.delegate = self;
    [imageV addGestureRecognizer:pinch];
    
    
    //添加旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    
    [imageV addGestureRecognizer:rotation];
}

//捏合的时候调用.
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    
    pinch.view.transform = CGAffineTransformScale( pinch.view.transform, pinch.scale, pinch.scale);
    // 复位
    pinch.scale = 1;
}


//旋转的时候调用
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    // 旋转图片
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    
    // 复位,只要想相对于上一次旋转就复位
    rotation.rotation = 0;
    
}


//拖动的时候调用
- (void)pan:(UIPanGestureRecognizer *)pan{
    
    CGPoint transP = [pan translationInView:pan.view];
    
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, transP.x, transP.y);
    //复位
    [pan setTranslation:CGPointZero inView:pan.view];
    
    
}


//能够同时支持多个手势
-(BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
}


//获取当前图层
-(UIImage *)getCurrentLayer{
    
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
